﻿// Copyright (c) 2013 Bryan C. O'Connell
// http://wsubi.codeplex.com/license
// Purpose: Reads and stores a Shortcut file into memory for use during runtime.

using System;
using System.IO;
using System.Collections.Generic;

namespace Wsubi
{
    class Shortcuts
    {
        /// <param name="FullFilepath">Path and name of the Shortcut file to be processed.</param>
        public Shortcuts(string FullFilepath)
        {
            if (File.Exists(FullFilepath)) 
            {
                _ShortcutsFile = FullFilepath;  

                ReadIntoMemory();
            }
            else 
            {
                throw new FileNotFoundException("So sorry, the Shortcut file you specified doesn't seem to exist.");
            }
        }

        /// <summary>Reads the current Shortcuts file and stores the values in memory.</summary>
        private void ReadIntoMemory()
        {
            _ShortcutLookup.Clear();

            using (StreamReader Reader = new StreamReader(_ShortcutsFile))
            {
                string Line;
                while ((Line = Reader.ReadLine()) != null)
                {
                    const char PIPE = '|';

                    const string ErrorMessage = "So sorry, the Shortcut file you specified is not formatted correctly.";

                    if (!Line.Contains(PIPE.ToString()))
                    {
                        throw new InvalidDataException(ErrorMessage);
                    }

                    string[] ShortcutEntry = Line.Split(PIPE);

                    if (ShortcutEntry.Length < 1)
                    {
                        throw new InvalidDataException(ErrorMessage);
                    }
                        
                    // send key to lower when stored in memory - to match what's done in itamae.
                    _ShortcutLookup.Add(ShortcutEntry[0].ToLower(), ShortcutEntry[1]);
                }
            }

            if (_ShortcutLookup.Count == 0)
            {
                throw new FileLoadException("The Shortcut file specified does not contain any commands.");
            }
        }

        /// <summary>Returns the value for the specified shortcut key.</summary>
        /// <param name="Key">The key to search for.</param>
        public string GetShortcutValue(string Key)
        {
            string ShortcutValue = string.Empty;

            // Key was saved as ToLower in ReadIntoMemory, so it needs to be searched by ToLower.
            if (_ShortcutLookup.ContainsKey(Key.ToLower()))
            {
                ShortcutValue = _ShortcutLookup[Key.ToLower()];
            }
            else
            {
                throw new KeyNotFoundException("So sorry, the shortcut you requested was not found.");
            }

            return ShortcutValue;
        }

        #region Properties / Variables

        // Current shortcuts file.
        private string _ShortcutsFile;

        // Stores the values from the current shortcuts file.
        private Dictionary<string, string> _ShortcutLookup = new Dictionary<string, string>();

        #endregion
    }
}
